package com.franz.noteapp.di

import android.app.Application
import androidx.room.Room
import androidx.room.RoomDatabase
import com.franz.noteapp.room.database.NoteDatabase
import com.franz.noteapp.room.operation.NoteRepositoryImpl
import com.franz.noteapp.room.repository.NoteRepository
import com.franz.noteapp.useCase.*
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton

/**
 * Module：用来管理所有需要提供的对象
 * Provides：用来提供对象
 * InstallIn：用来将模块装载到对应作用域饿，此处是单例
 * 自动绑定到"SingletonComponent::class"上*/
@Module
@InstallIn(SingletonComponent::class)
object AppModule {

    /**
     * 提供数据库对象*/
    @Provides
    @Singleton
    fun providerNoteDataBase(application: Application):NoteDatabase{
        return Room.databaseBuilder(
            application,
            NoteDatabase::class.java,
            NoteDatabase.DATABASE_NAME
        ).build()
    }


    /**
     * 提供数据库Dao类操作对象*/
    @Provides
    @Singleton
    fun providerNoteRepository(db:NoteDatabase):NoteRepository{
        return NoteRepositoryImpl(db.noteDao)
    }

    /**
     * 提供数据库具体操作内容对象*/
    @Provides
    @Singleton
    fun providerNoteUseCase(repository: NoteRepository):NoteUseCase{
        return NoteUseCase(
            GetNotes(repository),
            GetNote(repository),
            DeleteNote(repository),
            InsertNote(repository)
        )
    }
}